package com.gengzp.rpc.runner;

import com.gengzp.rpc.factory.RpcBeanInstanceFactory;
import com.gengzp.rpc.strategy.RpcRegisterCenterStrategy;
import com.gengzp.rpc.tools.RpcEnvironmentVariableReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @ClassName RpcCommandLineRunner
 * @Description 启动时初始化逻辑
 * @Author gengzp
 * @Date 2025/8/26 10:12
 */
@Component
public class RpcCommandLineRunner implements CommandLineRunner {

    @Autowired
    private RpcBeanInstanceFactory rpcBeanInstanceFactory;

    @Override
    public void run(String... args) {
        Optional.ofNullable(
                        rpcBeanInstanceFactory.getRpcRegisterCenterStrategy(
                                RpcEnvironmentVariableReader.getRegisterCenter().getName()))
                // 有对应的注册中心策略才会执行注册方法
                .ifPresent(RpcRegisterCenterStrategy::registerService);
    }

}
